<template>
  <basic-container>
    <avue-crud @on-load="onLoad" :option="option" :table-loading="loading" :data="data" :page.sync="page" v-model="form" ref="crud" @row-update="rowUpdate"  @row-save="rowSave" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @sort-change="sortChange">
      <template slot-scope="scope" slot="isEnable">
        <el-switch v-model="scope.row.isEnable" :disabled="true" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0">
        </el-switch>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { pageList,del,save,selectOne } from "@/api/rb-oem/posinfo/privateDy";

export default {
  data() {
    return {
      rpiNo: "",
      form: {},
      query: {},
      search: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      data: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: true,
        addBtn: true,
        delBtn: true,
        selection: false,//列表多选框
        searchMenuSpan: 24,
        columnBtn: false,
        labelWidth: 120, //弹出表单的标题的统一宽度
        searchLabelWidth: 120, //搜索框标题宽度
        // menuWidth: 100, //操作栏宽度
        emptyBtnText: '重置',
        // menu: false,
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            hide: true
          },
          {
            label: "名称",
            prop: "name",
            type: "input",
            // span: 24
            rules: [
              {
                required: true,
                message: '请输入',
                trigger: ['change','blur']
              }
            ]
          },
          {
            label: "内容",
            prop: "content",
            type: "textarea",
            span: 24,
            rules: [
              {
                required: true,
                message: '请输入',
                trigger: ['change','blur']
              }
            ]
          },
          
          {
            label: "是否启用",
            prop: "isEnable",
            type: "radio",
            dataType: "number",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            dicUrl: "/api/blade-system/dict/dictionary?code=yes_or_no",
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: ['change','blur']
              }
            ]
          }
        ]
      }
    };
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
    base64() {
      return (val) => {
        if(val){
          if (val.indexOf('data:image/png;base64,') == -1) {
            return ('data:image/png;base64,' + val );
          } else {
            return val;
          }
        }else{
          return ''
        }
      };
    }
  },
  created() {
    this.rpiNo = this.$route.query.rpiNo;
  },
  methods: {
    rowSave(row, done, loading) {
      row.rpiNo = this.$route.query.rpiNo;
      save(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      save(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    /* 列表删除 */
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return del(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    /**
     * @func 重置
     * @desc 重置查询条件
     * */
     searchReset() {
      this.query = {};
      this.search = {};
      this.onLoad(this.page);
    },
    /**
     * @func 搜索
     * @desc 根据条件查询
     * @param {params} params --- 条件查询
     * */
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    /**
     * @func 选中单元格
     * @desc 选中单元格数据
     * @param {list} list --- 选中单元格数据-数组
     * */
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    /**
     * @func 分页页码
     * @desc 分页页码改变时会触发
     * */
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      // this.refreshChange();
      this.onLoad(this.page, this.query);

    },
    /**
     * @func 当前页个数
     * @desc pageSize改变时会触发
     * */
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      // this.refreshChange();
      this.onLoad(this.page, this.query);
    },
    /**
     * @func 刷新
     * @desc 点击刷新按钮触发该事件
     * */
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    /**
     * @func 初始化
     * @desc 表格初始化查询数据
     * */
    onLoad(page, params = {}) {
      this.loading = true;
      let paramObj = Object.assign(params, this.query);
      paramObj.rpiNo = this.rpiNo;

      pageList(
        page.currentPage,
        page.pageSize,
        paramObj
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  },
};
</script>